<?php
namespace app\Logic;

use app\Logic\Layui\ServiceProviders\ServiceProviderInterface;

class Container
{
    public $values = [];

    public function __construct()
    {
        $this->registerProviders();
    }


    /**
     * @return $this
     */
    public function registerProviders()
    {
        foreach (static::$providers as $provider){

            $provider::register($this);
        }

        return $this;
    }


    /**
     * @param $name
     * @return mixed
     * @throws \Exception
     */
    public function __get($name)
    {
        if(!isset($this->values[$name])){

            throw new \Exception('该服务不存在');
        }

        return $this->values[$name];
    }


    /**
     * @param $name
     * @param $arguments
     * @return mixed
     * @throws \Exception
     */
    public function __call($name , $arguments) : ServiceProviderInterface
    {
        if(!isset($this->values[$name])){

            throw new \Exception('该服务不存在');
        }

        return $this->values[$name](...$arguments);
    }
}